﻿using ProtoBuf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sharp.Interprocess.MemoryManager
{
    /// <summary>
    /// Memory Manager's serialization helper --> for serializing internal structures. Uses Protobuf-net library for serialization
    /// </summary>
    internal static class SerializationUtil
    {
        internal static byte[] Serialize<T>(T objectToSerialize)
        {
            byte[] serializedData;
            using (var stream = new MemoryStream())
            {
                Serializer.Serialize(stream, objectToSerialize);
                serializedData = stream.ToArray();
            }
            return serializedData;
        }

        internal static T Deserialize<T>(byte[] serializedData)
        {
            T deserializedData;
            using (var stream = new MemoryStream(serializedData))
            {
                stream.Position = 0;
                deserializedData = Serializer.Deserialize<T>(stream);
            }

            return deserializedData;
        }
    }
}
